//
//  LZNotificationPresenter.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/16.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZNotificationPresenter.h"
#import "JPUSHService.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "LAMerchantDetailViewController.h"
#import "LZGrabCouponDetailViewController.h"
#import "LZFightGroupDetailViewController.h"
#import "LZCheckoutView.h"
#import "LAWriteReviewViewController.h"
#import "LZUserModel.h"
#import "LZLoginViewController.h"
#import "LZReceiveRestoreViewController.h"
#import "LZMsgCenterPresenter.h"
#import "LZMineViewController.h"
#import "LZUserInfoPresenter.h"
#import "AppDelegate.h"

@interface LZNotificationPresenter ()

@property (nonatomic, weak) LZCheckoutView *couponView;
@property (nonatomic, strong) LZUserInfoPresenter *userPresenter;

@end

@implementation LZNotificationPresenter

#pragma mark - public method

// 设置别名
+(void)setNotificationAlias:(NSString*)alias {
    if (alias.length>0) {
        [JPUSHService setAlias:alias callbackSelector:nil object:nil];
    }
}

// 移除别名
+(void)removeNotificationAlias {
    [JPUSHService setAlias:@"" callbackSelector:nil object:nil];
}

/**
 设置新的JPush 服务器角标数
 
 @param count <#count description#>
 */
+ (void)setNewBadgeCount:(NSInteger)count
{
    [JPUSHService setBadge:0];
}

- (void)dealNotificationWithDictionary:(NSDictionary *)notificationDic {
    
    DLog(@"%@",notificationDic);
    
//    NSString *title = notificationDic[@"title"];
//    NSString *content = notificationDic[@"content"];
    NSDictionary *extra = notificationDic[@"extras"];
    if ([extra[@"type"] isEqualToString:@"3"]) {
        // 结账类型的透传
        [self dealCheckOutTypeNotificationWithDictionary:notificationDic];
    }
    
}

#pragma mark - 透传相关

/* *************************************** 结账类型的透传 *************************************** */

- (void)dealCheckOutTypeNotificationWithDictionary:(NSDictionary *)notificationDic {

    NSDictionary *extra = notificationDic[@"extras"];

    UITabBarController *tabbarController = (UITabBarController*)[UIApplication sharedApplication].delegate.window.rootViewController;
    UINavigationController *nav = tabbarController.selectedViewController;
    UIViewController *lastVC = nav.viewControllers.lastObject;
    if ([lastVC isKindOfClass:[LAMerchantDetailViewController class]]) {
        /* ************* 如果当前页是  餐厅详情页 ************* */
        if (![(LAMerchantDetailViewController*)lastVC checkAndDealNotificaton:notificationDic]) {
            [self showPaySuccessViewWithPromotionId:extra[@"promotionId"] andSellerId:extra[@"sellerId"] andNoRemarkId:extra[@"noRemarkId"] andSuperView:nav.view andNavigationController:nav];
        }
    }
    else if ([lastVC isKindOfClass:[LZGrabCouponDetailViewController class]]) {
        /* ************* 如果当前页是  优惠券详情页 ************* */
        if (![(LZGrabCouponDetailViewController*)lastVC checkAndDealNotificaton:notificationDic]) {
            [self showPaySuccessViewWithPromotionId:extra[@"promotionId"] andSellerId:extra[@"sellerId"] andNoRemarkId:extra[@"noRemarkId"] andSuperView:nav.view andNavigationController:nav];
        }
    }
    else if ([lastVC isKindOfClass:[LZFightGroupDetailViewController class]]) {
        /* ************* 如果当前页是  团购详情页 ************* */
        if (![(LZFightGroupDetailViewController*)lastVC checkAndDealNotificaton:notificationDic]) {
            [self showPaySuccessViewWithPromotionId:extra[@"promotionId"] andSellerId:extra[@"sellerId"] andNoRemarkId:extra[@"noRemarkId"] andSuperView:nav.view andNavigationController:nav];
        }
    }
    else {
        
        [self showPaySuccessViewWithPromotionId:extra[@"promotionId"] andSellerId:extra[@"sellerId"] andNoRemarkId:extra[@"noRemarkId"] andSuperView:(nav.viewControllers.count>1)?nav.view:tabbarController.view andNavigationController:nav];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"checkOutNotification" object:extra];
}

- (void)showPaySuccessViewWithPromotionId:(NSString*)promotionId andSellerId:(NSString*)sellerId andNoRemarkId:(NSString*)noRemarkId andSuperView:(UIView*)aView andNavigationController:(UINavigationController*)navigationController {
    [aView addSubview:self.couponView];
    
    self.couponView.stateBlock = ^(PayCouponViewState state) {
        if (state == PayCouponViewState_Remove) {
            // 开启返回手势
            if (navigationController.viewControllers.count>1) {
                navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
            }
        }
    };
    
    self.couponView.reviewBlock = ^(NSString* sellerId,NSString *promotionId,NSString *noRemarkId) {
        // 点击评价按钮 进入评价页面
        
        if ([[LZUserModel Instance] isLogin]) {
            LAWriteReviewViewController *vc = [[LAWriteReviewViewController alloc] initWithNibName:@"LAWriteReviewViewController" bundle:nil] ;
            vc.type = WriteReviewType_Video;
            vc.sellerId = [sellerId copy];
            vc.videoId = [promotionId copy];
            vc.video_noRemarkId = [noRemarkId copy];
            
            if (navigationController.viewControllers.count == 1) {
                vc.hidesBottomBarWhenPushed = YES;
            }
            
            [navigationController pushViewController:vc animated:YES];
        }
        else {
            // 如果未登录
            LZLoginViewController * vc = [[LZLoginViewController alloc] init];
            if (navigationController.viewControllers.count == 1) {
                vc.hidesBottomBarWhenPushed = YES;
            }
            [navigationController pushViewController:vc animated:YES];
        }
        
    };
    
    [self.couponView scanQRCodeSuccessWithSellerId:sellerId andPromotionId:promotionId andNoRemarkId:noRemarkId];
    
    navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
}

#pragma mark - 处理 推送消息,当app在前台的时候
- (void)operationPushWhenAppInDeskWithInfo:(NSDictionary *)dic
{
    UITabBarController *tabbarController = (UITabBarController*)[UIApplication sharedApplication].delegate.window.rootViewController;
    UINavigationController *nav = tabbarController.selectedViewController;
    UIViewController *lastVC = nav.viewControllers.lastObject;
    
    if ([lastVC isKindOfClass:[LZMineViewController class]]) {
        [(LZMineViewController *)lastVC getUserCenterInfo];
    }
    if (_userPresenter == nil) {
        _userPresenter = [[LZUserInfoPresenter alloc] init];
    }

    [self.userPresenter checkUserHaveUnReadMsg:^(NSInteger unReadCount) {
        AppDelegate *d = (AppDelegate *)[UIApplication sharedApplication].delegate;
        if (unReadCount > 0) {
            [d showTabbarBadge];
        }else{
            [d hiddenTabbarBadge];
        }
    } failure:^(NSString *fail) {
        AppDelegate *d = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [d hiddenTabbarBadge];
    }];

}

#pragma mark - 处理apns push到相关详情页
- (void)operationPushToVCWithInfo:(NSDictionary *)info
{
    UITabBarController *tabbarController = (UITabBarController*)[UIApplication sharedApplication].delegate.window.rootViewController;
    UINavigationController *nav = tabbarController.selectedViewController;
    nav.hidesBottomBarWhenPushed = YES;
    
    if (![[LZUserModel Instance] isLogin]) {
        // 如果未登录
        LZLoginViewController * vc = [[LZLoginViewController alloc] init];
        [nav pushViewController:vc animated:YES];
        
        return;
    }
    
    NSInteger msgType = [info[@"type"] integerValue];
    NSString *bussinessId = info[@"bussinessId"];
    NSString *msgId = info[@"msgId"];
    [self changeMsgIsReaded:msgId];
    
    switch (msgType) {
        case kMsgCenterType_customerResComment:{//餐厅评论
            LZReceiveRestoreViewController *vc = [[LZReceiveRestoreViewController alloc] initWithNibName:@"LZReceiveRestoreViewController" bundle:nil];
            vc.reviewType = ReviewType_Restaurants;
            vc.replyId = bussinessId;
            vc.hidesBottomBarWhenPushed = YES;
            [nav pushViewController:vc animated:YES];
        }
            break;
        case kMsgCenterType_customerVideoComment:{//视频评论
            LZReceiveRestoreViewController *vc = [[LZReceiveRestoreViewController alloc] initWithNibName:@"LZReceiveRestoreViewController" bundle:nil];
            vc.reviewType = ReviewType_Video;
            vc.replyId = bussinessId;
            vc.hidesBottomBarWhenPushed = YES;
            [nav pushViewController:vc animated:YES];
        }
            break;
        case kMsgCenterType_couponExpired:{//优惠券过期提醒
            LZGrabCouponDetailViewController *vc = [[LZGrabCouponDetailViewController alloc] initWithNibName:@"LZGrabCouponDetailViewController" bundle:nil];
            vc.videoID = bussinessId;
            vc.couponState = kCoupon_notUsed;
            vc.type = CouponType_MessageCenter;
            vc.hidesBottomBarWhenPushed = YES;
            [nav pushViewController:vc animated:YES];
        }
            break;
        case kMsgCenterType_groupExpired:{//团购券过期提醒
            LZFightGroupDetailViewController *vc = [[LZFightGroupDetailViewController alloc] initWithNibName:@"LZFightGroupDetailViewController" bundle:nil];
            vc.videoID = bussinessId;
            vc.couponState = kCoupon_notUsed;
            vc.type = GroupType_MessageCenter;
            vc.hidesBottomBarWhenPushed = YES;
            [nav pushViewController:vc animated:YES];
            
        }
            break;
        case kMsgCenterType_groupSuccess:{//成团提醒
            LZFightGroupDetailViewController *vc = [[LZFightGroupDetailViewController alloc] initWithNibName:@"LZFightGroupDetailViewController" bundle:nil];
            vc.videoID = bussinessId;
            vc.couponState = kCoupon_notUsed;
            vc.type = GroupType_MessageCenter;
            vc.hidesBottomBarWhenPushed = YES;
            [nav pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }

    
}

/**
 此条消息已经读取.
 
 @param msgId <#msgId description#>
 */
- (void)changeMsgIsReaded:(NSString *)msgId
{
    LZMsgCenterPresenter *p = [[LZMsgCenterPresenter alloc] init];
    __block NSInteger cc = [[UIApplication sharedApplication] applicationIconBadgeNumber];
    [p changeMsgStateReadedWithMsgID:msgId Success:^(id data) {
        cc -- ;
        [LZNotificationPresenter setNewBadgeCount:cc];
        if (cc == 0) {
            AppDelegate *d = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [d hiddenTabbarBadge];
        }
        
    } Failure:^(NSString *fail) {
        
    }];
}


#pragma mark - 控件初始化

- (LZCheckoutView*)couponView {
    if (_couponView) {
        return _couponView;
    }
    
    UITabBarController *tabbarController = (UITabBarController*)[UIApplication sharedApplication].delegate.window.rootViewController;

    LZCheckoutView *v = [[LZCheckoutView alloc] initWithFrame:tabbarController.view.bounds];
    
    _couponView = v;
    return _couponView;
}


@end
